package com.harth.blemultiplatform.ble

import com.harth.blemultiplatform.ble.core.Phy
import com.harth.blemultiplatform.ble.core.PrimaryPhy
import kotlinx.coroutines.CoroutineScope
import no.nordicsemi.kotlin.ble.client.android.AdvertisingData
import no.nordicsemi.kotlin.ble.client.android.ScanResult

class NativeScanResult(val scanResult: ScanResult, val scope: CoroutineScope) : IScanResult {

    override val IPeripheral: IPeripheral
        get() = NativePeripheral(
            peripheral = scanResult.peripheral,
            scope = scope
        )

    override val isConnectable: Boolean
        get() = scanResult.isConnectable

    override val advertisingData: IAdvertisingData
        get() = NativeAdvertisingData(scanResult.advertisingData)

    override val rssi: Int
        get() = scanResult.rssi

    override val txPowerLevel: Int?
        get() = scanResult.txPowerLevel

    override val primaryPhy: PrimaryPhy
        get() = when (scanResult.primaryPhy) {
            no.nordicsemi.kotlin.ble.core.PrimaryPhy.PHY_LE_1M -> PrimaryPhy.PHY_LE_1M
            no.nordicsemi.kotlin.ble.core.PrimaryPhy.PHY_LE_CODED -> PrimaryPhy.PHY_LE_CODED
        }

    override val secondaryPhy: Phy?
        get() = scanResult.secondaryPhy?.toPhy()

    override val timestamp: Long
        get() = scanResult.timestamp
}